package com.molyfun.parents.im.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.TeacherViewer;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class RecommendProductCellView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.teacher_list)
    TeacherViewer mTeacherViewer;

    @InjectView(R.id.price_now)
    TextView mPriceNow;
    @InjectView(R.id.people_study_count)
    TextView mStudyCount;
    @InjectView(R.id.line)
    View mLine;
    private Context context;
    private ChatMessageEntity mChatMessageEntity;

    public RecommendProductCellView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public RecommendProductCellView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RecommendProductCellView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_chat_recommend_product, this);
        ButterKnife.inject(this);
    }

    public void initData(ChatMessageEntity chatMessageEntity) {
        initData(chatMessageEntity, false);
    }

    public void initData(ChatMessageEntity chatMessageEntity, boolean isLast) {
        if(chatMessageEntity == null){
            return;
        }
        mChatMessageEntity = chatMessageEntity;
        mProductName.setText(chatMessageEntity.getExtendfield2());
        mPriceNow.setText("￥" + chatMessageEntity.getExtendfield3());
        mStudyCount.setText(chatMessageEntity.getExtendfield5());
        mTeacherViewer.initData(chatMessageEntity.getUserlist());
        if(isLast){
            mLine.setVisibility(View.GONE);
        }else{
            mLine.setVisibility(View.VISIBLE);
        }
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        ProductDetailActivity.startActivity(getContext(), mChatMessageEntity.getExtendfield1(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
    }


}